#include<stdio.h>
#include "../include/vfs.h"
#include<stdlib.h>
#include<string.h>


int overwrite(FD *dest, FD *src)
{
	FILE *fp;
	FD *src_exist,*dest_exist;
	long src_block,dest_block,recsize;
	int i,j;
	char *src_data;
	Block *b;

	fp = fopen(filesystem_name,"r+b");

	header=malloc(sizeof(Header));

	fread(header,sizeof(Header),1,fp);

	b = malloc(sizeof(Block));
	
	recsize = sizeof(Block);

	src_exist = search(roots.naryroot,src);
	dest_exist = search(roots.naryroot,dest);

	src_data = malloc(sizeof(char)*1024);
	

	src_block=src_exist->block_no;

		
			for(i=0;i<header->num_blocks;i++)
			{
				fread(b,sizeof(Block),1,fp);
			
				if (b->num==src_block)					
				{
					strcpy(src_data,b->buffer);
					
					break;
				}
			}

	dest_block=dest_exist->block_no;

			rewind(fp);
			fread(header,sizeof(Header),1,fp);
		
			for(i=0;i<header->num_blocks;i++)
			{
				fread(b,sizeof(Block),1,fp);
			
				if (b->num==dest_block)					
				{	
					//printf("\n SRC DATA:%s",src_data);
					strcpy(b->buffer,src_data);
					
					break;
				}
			}


		rewind(fp);
		fread(header,sizeof(Header),1,fp);
		
		fseek(fp,(b->num-1)*recsize,SEEK_CUR); 
		fwrite(b,sizeof(Block),1,fp);

		fclose(fp);

}













